Skip to content

Add manual profile triggering and cooldown mechanism#4730

Draft
johnoliver wants to merge 3 commits into
microsoft:mainfrom
johnoliver:manual-profile-2
Draft

Add manual profile triggering and cooldown mechanism#4730
johnoliver wants to merge 3 commits into
microsoft:mainfrom
johnoliver:manual-profile-2

Conversation

@johnoliver
Copy link
Copy Markdown
Member

This pull request adds support for triggering manual profiling triggering profiles via 2 routes:

  • Creating/Touching a file at a well known location, by default "<temp_dir>/applicationinsights-agent-profile-trigger"
  • Add a JMX mbean, can be called via "com.microsoft:type=AI-alert,name=ProfilerControl triggerProfile"

Additionally this pr adds a global cooldown on the profile to ensure that we dont get repeat profiles from multiple sources

File-based manual profiling trigger:

  • Introduced AlertingProfileFileTriggerConfiguration, a new configuration class that controls the file-based manual profile trigger, including enablement, file path resolution, and default profile duration.
  • Updated AlertingSubsystem to support the file-based trigger: checks for the presence and recency of the trigger file, deletes it after use, and initiates a manual profile when appropriate. The subsystem now accepts the file trigger configuration as a dependency and has been refactored to evaluate both collection plan and file-based triggers. [1] [2] [3] [4] [5]

Testing enhancements:

  • Added AlertingSubsystemFileTriggerTest, a new test suite covering scenarios for the file-based trigger, such as firing when the file exists and is recent, not firing when disabled, not firing for old or missing files, and correct profile duration selection.
  • Updated existing tests (AlertingSubsystemTest) to use the new configuration and maintain compatibility. [1] [2] [3] [4]
  • Enhanced TestTimeSource to allow setting the current time, supporting precise control in tests.

Documentation:

  • Clarified in request-triggers.md that a global cooldown is enforced after any profile recording, affecting all trigger types, including the new file-based manual trigger.

- Creating/Touching a file at a well known location, by default "<temp_dir>/applicationinsights-agent-profile-trigger"
- Add a JMX mbean, can be called via "jcmd <pid> MBean.invoke com.microsoft:type=AI-alert,name=ProfilerControl triggerProfile"
- Add a global cooldown on the profile to ensure that we dont get repeat profiles from multiple sources
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds operator-initiated profiling to the Application Insights Java agent (file-based trigger + JMX MBean) and introduces a cross-trigger global cooldown to reduce rapid back-to-back recordings from different trigger sources.

Changes:

  • Added manual profile triggering via (1) a “touch file” trigger and (2) a JMX MBean (ProfilerControl).
  • Added a global profiler cooldown (globalCooldownSeconds) enforced across CPU/memory/request/manual/periodic triggers.
  • Updated docs and tests, including new test suites for file-trigger behavior and global cooldown.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
docs/README.md Documents new profiler settings and manual triggering mechanisms.
agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/triggers/GcEventInitTest.java Updates test wiring for new AlertingSubsystem.create(...) signature.
agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/triggers/AlertTriggerSpanProcessorTest.java Updates test wiring for new AlertingSubsystem.create(...) signature.
agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/ProfilerGlobalCooldownTest.java Adds tests for new global cooldown behavior.
agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/ProfilerControlTest.java Adds tests for JMX MBean registration and triggering behavior.
agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/triggers/AlertingSubsystemInit.java Wires file-trigger config into alerting subsystem and optionally registers the profiler control MBean.
agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/ProfilingInitializer.java Ensures file-trigger evaluation is performed on the config polling cycle.
agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/ProfilerControlMBean.java Introduces the JMX MBean interface for manual triggering.
agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/ProfilerControl.java Implements and registers the JMX MBean that emits MANUAL alert breaches.
agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/Profiler.java Adds global cooldown enforcement and a TimeSource for deterministic time handling.
agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/PerformanceMonitoringService.java Builds/resolves the file-trigger config and exposes evaluateFileTrigger().
agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java Adds config fields for manual settings, global cooldown, MBean enablement, and file trigger settings.
agent/agent-profiler/request-triggers.md Notes that global cooldown suppresses all triggers.
agent/agent-profiler/agent-alerting/src/test/java/com/microsoft/applicationinsights/alerting/TestTimeSource.java Adds ability to set “now” for tests.
agent/agent-profiler/agent-alerting/src/test/java/com/microsoft/applicationinsights/alerting/AlertingSubsystemTest.java Updates test wiring for new AlertingSubsystem.create(...) signature.
agent/agent-profiler/agent-alerting/src/test/java/com/microsoft/applicationinsights/alerting/AlertingSubsystemFileTriggerTest.java Adds test coverage for file-trigger scenarios.
agent/agent-profiler/agent-alerting/src/main/java/com/microsoft/applicationinsights/alerting/AlertingSubsystem.java Implements file-trigger evaluation and refactors manual-trigger evaluation.
agent/agent-profiler/agent-alerting-api/src/main/java/com/microsoft/applicationinsights/alerting/config/AlertingProfileFileTriggerConfiguration.java Introduces configuration object for the file-based manual trigger.
Comments suppressed due to low confidence (1)

agent/agent-profiler/agent-alerting/src/main/java/com/microsoft/applicationinsights/alerting/AlertingSubsystem.java:181

  • evaluateCollectionPlanTrigger() uses Instant.now() for expiration checks while the rest of the alerting subsystem uses the injected TimeSource. This makes behavior inconsistent (and harder to test) when a non-default TimeSource is provided. Consider using timeSource.getNow() here as well.
    boolean shouldTrigger =
        config.isSingle()
            && config.getMode() == EngineMode.immediate
            && Instant.now().isBefore(config.getExpiration())
            && !manualTriggersExecuted.contains(config.getSettingsMoniker());

Comment on lines +236 to +239
CollectionPlanConfiguration collectionPlan = alertConfig.getCollectionPlanConfiguration();
int durationSeconds = collectionPlan.getImmediateProfilingDurationSeconds();
if (durationSeconds <= 0) {
durationSeconds = alertingProfileFileTriggerConfiguration.getDefaultProfileDurationSeconds();
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add a null check

Comment on lines +62 to +66
FlightRecorderConnection frc = mock(FlightRecorderConnection.class);
when(frc.newRecording(any(), any())).thenReturn(mock(Recording.class));

profiler.initialize(mock(UploadService.class), Executors.newScheduledThreadPool(1), frc);
return profiler;
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants